package com.wyq.miaosha.controller;

import com.wyq.miaosha.domain.MiaoshaOrder;
import com.wyq.miaosha.domain.MiaoshaUser;
import com.wyq.miaosha.domain.OrderInfo;
import com.wyq.miaosha.rabbitmq.MQSender;
import com.wyq.miaosha.rabbitmq.MiaoshaMessage;
import com.wyq.miaosha.redis.*;
import com.wyq.miaosha.result.CodeMsg;
import com.wyq.miaosha.result.Result;
import com.wyq.miaosha.service.GoodsService;
import com.wyq.miaosha.service.MiaoshaService;
import com.wyq.miaosha.service.MiaoshaUserService;
import com.wyq.miaosha.service.OrderService;
import com.wyq.miaosha.util.MD5Util;
import com.wyq.miaosha.util.UUIDUtil;
import com.wyq.miaosha.vo.GoodsVo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/miaosha")
public class MiaoshaController implements InitializingBean {

	@Autowired
	MiaoshaUserService userService;
	
	@Autowired
	RedisService redisService;
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	MiaoshaService miaoshaService;

	@Autowired
	MQSender mqSender;

	private Map<Long,Boolean> localOverMap = new HashMap<Long, Boolean>();

	/**
	 * orderId
	 * 1：成功
	 * 0：派对中
	 * -1：秒杀失败
	 */
	@RequestMapping(value="/result", method= RequestMethod.GET)
	@ResponseBody
	public Result<Long> miaoshaResult(Model model,MiaoshaUser user,
								@RequestParam("goodsId")long goodsId) {
		model.addAttribute("user",user);
		if(user == null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		long result = miaoshaService.getMiaoshaResult(user.getId(), goodsId);
		return Result.success(result);
	}
	/**
	 * qps 1819
	 */
	@RequestMapping(value="/{path}/do_miaosha", method= RequestMethod.POST)
	@ResponseBody
	public Result<Integer> miaosha(Model model, MiaoshaUser user,
									 @RequestParam("goodsId")long goodsId,
									@PathVariable("path") String path) {
		model.addAttribute("user", user);
		if(user == null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		//验证path
		if(miaoshaService.checkPath(user,goodsId,path)){
			return Result.error(CodeMsg.MIAO_SHA_OVER);
		}
		if(!localOverMap.get(goodsId)){
			return Result.error(CodeMsg.MIAO_SHA_OVER);
		}
		//预减库存
		long stocak = redisService.decr(GoodsKey.getMiaoshaGoodsStock,""+goodsId);
		if(stocak < 0){
			localOverMap.put(goodsId, true);
			return Result.error(CodeMsg.MIAO_SHA_OVER);
		}
		//入队
		MiaoshaMessage miaoshaMessage = new MiaoshaMessage();
		miaoshaMessage.setMiaoshaUser(user);
		miaoshaMessage.setGoodsId(goodsId);
		mqSender.sendMiaoshaMessage(miaoshaMessage);
		return Result.success(0);
	}

	/**
	 * 系统初始化时操作
	 * @throws Exception
	 */
	public void afterPropertiesSet() throws Exception {
		List<GoodsVo> goodsVos = goodsService.listGoodsVo();
		if(goodsVos == null){
			return;
		}
		for (GoodsVo g : goodsVos){
			redisService.set(GoodsKey.getMiaoshaGoodsStock,""+g.getId(),g.getStockCount());
			localOverMap.put(g.getId(), false);
		}
	}

	@RequestMapping(value="/path", method= RequestMethod.GET)
	@ResponseBody
	public Result<String> getMiaoshaPath(Model model,MiaoshaUser user,
									  @RequestParam("goodsId")long goodsId) {
		model.addAttribute("user",user);
		if(user == null) {
			return Result.error(CodeMsg.SESSION_ERROR);
		}
		return Result.success(miaoshaService.createMiaoshaPath(user,goodsId));
	}
}
